<?php

namespace statistics\view\renderers;

/**
 * ****************************************************************************
 * Creates SelectBox for statistics-editor.
 *
 * Copyright : (c) 2004 - 2013 The Admidio Team
 * Homepage : http://www.admidio.org
 *
 * License : GNU Public License 2 http://www.gnu.org/licenses/gpl-2.0.htm
 * ***************************************************************************
 */
class SelectBoxRenderer {
	private $zeroValue;
	private $zeroText;
	private $showGrouping;
	private $showMasterData;
	private $showRoleInformation;
	private $defaultEntry;
	
	// TODO: HTML specific markup should be swapped out..
	private $HTMLAttributes;
	private $profileFields;
	private $languageObject;
	private $currentUser;
	public function __construct() {		
		global $gProfileFields;
		global $gCurrentUser;
		global $gL10n;
		
		$this->profileFields = $gProfileFields;
		$this->languageObject = $gL10n;
		$this->currentUser = $gCurrentUser;
		
		$this->initialize();
	}
	public function initialize() {
		$this->zeroValue = '';
		$this->zeroText = $this->languageObject->get ( 'SYS_PLEASE_CHOOSE' );
		$this->showGrouping = false;
		$this->showMasterData = false;
		$this->showRoleInformation = false;
		$this->defaultEntry = - 1;
		$this->HTMLAttributes = '';
	}
	public function generateProfileFieldSelectBox($HTMLId) {
		$user_fields = array ();
		
		$i = 1;
		$oldCategoryNameIntern = '';
		$posEndOfMasterData = 0;
		
		foreach ( $this->profileFields->mProfileFields as $field ) {
			// at the end of category master data save positions for loginname and username
			// they will be added after profile fields loop
			if ($this->showMasterData == true && $oldCategoryNameIntern == 'MASTER_DATA' && $field->getValue ( 'cat_name_intern' ) != 'MASTER_DATA') {
				$posEndOfMasterData = $i;
				$i = $i + 2;
			}
			
			// add profile field to user field array
			if ($field->getValue ( 'usf_hidden' ) == 0 || $this->currentUser->editUsers ()) {
				
				// $user_fields[$i] = new Object();
				$user_fields [$i] ['cat_id'] = $field->getValue ( 'cat_id' );
				$user_fields [$i] ['cat_name'] = $field->getValue ( 'cat_name' );
				$user_fields [$i] ['usf_id'] = $field->getValue ( 'usf_id' );
				$user_fields [$i] ['usf_name'] = addslashes ( $field->getValue ( 'usf_name' ) );
				$user_fields [$i] ['usf_name_intern'] = addslashes ( $field->getValue ( 'usf_name_intern' ) );
				
				$oldCategoryNameIntern = $field->getValue ( 'cat_name_intern' );
				$i ++;
			}
		}
		
		// Add loginname and photo at the end of category master data
		// add new category with start and end date of role membership
		if ($this->showMasterData == true) {
			
			if ($posEndOfMasterData == 0) {
				$posEndOfMasterData = $i;
				$i = $i + 2;
			}
			
			// $user_fields[$posEndOfMasterData] = new Object();
			$user_fields [$posEndOfMasterData] ['cat_id'] = $user_fields [1] ['cat_id'];
			$user_fields [$posEndOfMasterData] ['cat_name'] = $user_fields [1] ['cat_name'];
			$user_fields [$posEndOfMasterData] ['usf_id'] = 'usr_login_name';
			$user_fields [$posEndOfMasterData] ['usf_name'] = $this->languageObject->get ( 'SYS_USERNAME' );
			$user_fields [$posEndOfMasterData] ['usf_name_intern'] = $this->languageObject->get ( 'SYS_USERNAME' );
			
			// $user_fields[$posEndOfMasterData+1] = new Object();
			$user_fields [$posEndOfMasterData + 1] ['cat_id'] = $user_fields [1] ['cat_id'];
			$user_fields [$posEndOfMasterData + 1] ['cat_name'] = $user_fields [1] ['cat_name'];
			$user_fields [$posEndOfMasterData + 1] ['usf_id'] = 'usr_photo';
			$user_fields [$posEndOfMasterData + 1] ['usf_name'] = $this->languageObject->get ( 'PHO_PHOTO' );
			$user_fields [$posEndOfMasterData + 1] ['usf_name_intern'] = $this->languageObject->get ( 'PHO_PHOTO' );
		}
		
		if ($this->showRoleInformation) {
			// $user_fields[$i] = new Object();
			$user_fields [$i] ['cat_id'] = - 1;
			$user_fields [$i] ['cat_name'] = $this->languageObject->get ( 'LST_ROLE_INFORMATION' );
			$user_fields [$i] ['usf_id'] = 'mem_begin';
			$user_fields [$i] ['usf_name'] = $this->languageObject->get ( 'LST_MEMBERSHIP_START' );
			$user_fields [$i] ['usf_name_intern'] = $this->languageObject->get ( 'LST_MEMBERSHIP_START' );
			
			$i ++;
			
			// $user_fields[$i] = new Object();
			$user_fields [$i] ['cat_id'] = - 1;
			$user_fields [$i] ['cat_name'] = $this->languageObject->get ( 'LST_ROLE_INFORMATION' );
			$user_fields [$i] ['usf_id'] = 'mem_end';
			$user_fields [$i] ['usf_name'] = $this->languageObject->get ( 'LST_MEMBERSHIP_END' );
			$user_fields [$i] ['usf_name_intern'] = $this->languageObject->get ( 'LST_MEMBERSHIP_END' );
		}
		
		// generateHtml
		$category = '';
		
		$selectBoxHtml = '<select size="1" id="' . $HTMLId . '" name="' . $HTMLId . '" ' . $this->HTMLAttributes . '>';
		
		$selectBoxHtml .= $this->renderZeroOption ();
		
		for($counter = 1; $counter <= count ( $user_fields ); $counter ++) {
			if ($this->showGrouping == true && $category != $user_fields [$counter] ['cat_name']) {
				if ($category != '') {
					$selectBoxHtml .= '</optgroup>';
				}
				$selectBoxHtml .= '<optgroup label="' . $user_fields [$counter] ['cat_name'] . '">';
				$category = $user_fields [$counter] ['cat_name'];
			}
			$selectBoxHtml .= $this->renderOption ( $user_fields [$counter] ['usf_id'], $user_fields [$counter] ['usf_id'], $user_fields [$counter] ['usf_name'] );
		}
		if ($this->showGrouping == true) {
			$selectBoxHtml .= '</optgroup>';
		}
		$selectBoxHtml .= '</select>';
		
		return $selectBoxHtml;
	}
	public function generateStatisticConfigSelectBox($entryArray, $HTMLId) {
		$selectBoxHtml = '<select size="1" id="' . $HTMLId . '" name="' . $HTMLId . '" ' . $this->HTMLAttributes . '>';
		$selectBoxHtml .= $this->renderZeroOption ();
		
		$value = reset ( $entryArray );
		for($arrayCount = 0; $arrayCount < count ( $entryArray ); $arrayCount ++) {
			$selectBoxHtml .= $this->renderOption ( $arrayCount, key ( $entryArray ), $value );
			$value = next ( $entryArray );
		}
		
		$selectBoxHtml .= '</select>';
		return $selectBoxHtml;
	}
	
	private function renderZeroOption() {
		$optionHtml = '';
		if (strlen ( $this->zeroValue ) != 0) {
			$optionHtml .= $this->renderOption ( - 1, $this->zeroValue, $this->zeroText );
		}
		return $optionHtml;
	}
	private function renderOption($EntryNr, $value, $text) {
		$optionHtml = '<option value="' . $value . '"';
		if ($EntryNr == $this->defaultEntry) {
			$optionHtml .= ' selected="selected" ';
		}
		$optionHtml .= '>' . $text . '</option>';
		return $optionHtml;
	}
	public function setZeroValue($zeroValue) {
		$this->zeroValue = $zeroValue;
	}
	public function setZeroText($zeroText) {
		$this->zeroText = $zeroText;
	}
	public function setShowGrouping($grouping) {
		$this->showGrouping = $grouping;
	}
	public function setShowMasterData($masterData) {
		$this->showMasterData = $masterData;
	}
	public function setShowRoleInformation($roleInformation) {
		$this->showRoleInformation = $roleInformation;
	}
	public function setSelectedEntry($defaultEntry) {
		$this->defaultEntry = $defaultEntry;
	}
	public function setHTMLAttributes($HTMLAttributes) {
		$this->HTMLAttributes = $HTMLAttributes;
	}
}

?>